package com.sina.shop.service.collect;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.dao.collect.CollectGoodsDao;
import com.sina.shop.domain.collect.CollectGoods;
import com.sina.shop.service.ShopDataSourceService;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-06-15:55
 */
@Service("collectGoodsService")
public class CollectGoodsServiceImpl extends ShopDataSourceService implements CollectGoodsService {

    @Autowired
    @Qualifier("collectGoodsDao")
    private CollectGoodsDao collectGoodsDao;

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public List<CollectGoods> getCollectGoodsListByAdminId(Long adminId) {
        return collectGoodsDao.getCollectGoodsListByAdminId(adminId);
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public Integer deleteCollectGoods(Long id) throws DataException, UpdateException {
        CollectGoods collectGoods = collectGoodsDao.get(CollectGoods.class, id);
        Assert.assertNotNull(collectGoods, "CollectGoods[id=%s] not exists", collectGoods.getId());

        try {
            return collectGoodsDao.deleteCollectGoods(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete CollectGoods error.", CollectGoods.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public CollectGoods saveCollectGoods(CollectGoods collectGoods) throws CreateException, ResourceExistsException, DataException {
        try {
            if (collectGoods.getGoods() != null && collectGoods.getAdmin() != null) {
                if (collectGoodsDao.getCollectGoodsByAidAndBid(
                        collectGoods.getAdmin().getId(),
                        collectGoods.getGoods().getId()
                ) != null) {
                    throw new DataAccessResourceFailureException("收藏失败，你已收藏过！！");
                }
            }
            collectGoodsDao.save(collectGoods);
            collectGoodsDao.flush();
            return collectGoods;
        } catch (DataAccessException e) {
            LOG.error(" {} save CollectGoods error.", CollectGoods.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, readOnly = true, isolation = Isolation.READ_COMMITTED)
    public CollectGoods getCollectGoodsByAidAndBid(Long adminId, Long goodsId) {
        return collectGoodsDao.getCollectGoodsByAidAndBid(adminId, goodsId);
    }
}
